<?php

namespace app\api\controller;

use org\Jdk_inc;
use think\Controller;
use think\Db;

error_reporting(0);
header("Access-Control-Allow-Origin:*");
class Sendtemp extends Controller
{
    
    //发送模板消息
    public function sendMessage($template)
    {   
        $jdk = new Jdk_inc();
        
        //获取access_token
        $access_token = $jdk->getToken(); 
         
        $json_template = json_encode($template);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token;
        $res = $this->httpPost($url, $json_template);      //以post方式 向用户发送数据
        return $res;
    }
    
    //模板消息
    public function httpPost($url,$data) {
    	$curl = curl_init();
    	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    	curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    	curl_setopt($curl, CURLOPT_URL, $url);
    	curl_setopt($curl, CURLOPT_POST, true);
    	curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
    	$res = curl_exec($curl);
    	curl_close($curl);
    	return $res;
    }
 

}
